package org.mspring.mlog.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.mspring.mlog.dao.UserDao;
import org.mspring.mlog.entity.Link;
import org.mspring.mlog.entity.SysUser;
import org.mspring.mlog.entity.User;
import org.mspring.platform.utils.StringUtils;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl extends BaseDaoImpl implements UserDao{

	@Override
	public SysUser login(String username, String password) {
		// TODO Auto-generated method stub
        // 密码加密
        password = StringUtils.getMD5(password);

        SysUser user = new SysUser();
        user.setLOGIN_NAME(username);
        user.setPASSWORD(password);
        
        // 初始化连接
        SqlSession sqlSession = getSqlSession();
        // 执行sql
		List<SysUser> userInfoList = (List<SysUser>) sqlSession
				.selectList(
						"LoginWidget.adminLoginAct.checkSysAdmin",
						user);
		
		// 断开连接
		sqlSession.close();
		
        if (userInfoList != null && userInfoList.size() > 0) {
            return (SysUser) userInfoList.get(0);
        }
        
		return null;
	}

	@Override
	public SysUser getUserByName(String username) {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		SysUser user = new SysUser();
		user.setLOGIN_NAME(username);
		// 执行sql
		List<SysUser> list = (List<SysUser>) sqlSession.selectList(
				"LoginWidget.adminLoginAct.getUserByName", user);

		// 断开连接
		sqlSession.close();
		
		if (null != list && list.size() > 0) {
			
			return (SysUser) list.get(0);
		}

		return null;
	}

	@Override
	public List<SysUser> getAllUser() {
		// 初始化连接
		SqlSession sqlSession = getSqlSession();
		// 执行sql
		List<SysUser> list = (List<SysUser>) sqlSession.selectList(
				"LoginWidget.adminLoginAct.getAllUser");

		// 断开连接
		sqlSession.close();
		return list;
	}

}
